Type XMLCalMaterialLoader Extends CalLoader

	Method Load:Object(data:Object)
		Local isimvu:Byte = False
		
		Local databank:TBank = TBank(data)
		If databank = Null Then
		
			databank = LoadBank(data.ToString())
			If databank = Null Then Return Null
		EndIf
		
		
		Local doc:xmlDocument = New xmlDocument
		doc.Load(databank)
				
		If doc = Null Then
			DebugLog "Could not parse XML"
			loaded = True
			Return Null
		EndIf
		
		Local materialElem:xmlNode = doc.Root()
		
		If materialElem = Null Then 
			DebugLog "Material Node not found!"
			loaded = True
			Return Null
		EndIf
		
		If Lower(materialElem.name) <> "material" Then
			If Lower(materialElem.name) = "header" Then
				materialElem = materialElem.NextSibling()
				isimvu = True
				If Lower(materialElem.name) <> "material" Then
					DebugLog "Wrong Start Node: "+materialElem.name
					loaded = True
					Return Null				
				EndIf
			Else
				DebugLog "Wrong Start Node: "+materialElem.name
				loaded = True
				Return Null	
			EndIf
		EndIf	
		
		Local coreMaterial:CalCoreMaterial = New CalCoreMaterial
		
		Local ambientElem:xmlNode = materialElem.FindChild("AMBIENT")
		Local acol:String[] = ambientElem.value.Split(" ")
		Local ambientColor:CalColor = CalColor.FromValues(Int(acol[0]),Int(acol[1]),Int(acol[2]),Int(acol[3]))

		Local diffuseElem:xmlNode = materialElem.FindChild("DIFFUSE")
		Local dcol:String[] = diffuseElem.value.Split(" ")
		Local diffuseColor:CalColor = CalColor.FromValues(Int(dcol[0]),Int(dcol[1]),Int(dcol[2]),Int(dcol[3]))

		Local specularElem:xmlNode = materialElem.FindChild("SPECULAR")
		Local scol:String[] = specularElem.value.Split(" ")
		Local specularColor:CalColor = CalColor.FromValues(Int(scol[0]),Int(scol[1]),Int(scol[2]),Int(scol[3]))
		
		Local shininessElem:xmlNode = materialElem.FindChild("SHININESS")
		
		Local shininess:Float = Float(shininessElem.value)
		coreMaterial.setAmbientColor(ambientColor)
		coreMaterial.setDiffuseColor(diffuseColor)
		coreMaterial.setSpecularColor(specularColor)
		coreMaterial.setShininess(shininess)
		
		'Local MapIds:List<xmlNode> = materialElem.GetChildren("map")
		coreMaterial.reserve(Int(materialElem.Attribute("NUMMAPS").Value))
		Local mapId:Int = 0
		Local mapElem:xmlNode = materialElem.FirstChild()
		While mapElem <> Null
		'For Local mapElem:xmlNode = EachIn MapIds
			If Lower(mapElem.Name) = "map" Then
				Local map:CalMap = New CalMap
				map.filename = mapElem.value
				coreMaterial.setMap(mapId, map)
				mapId = mapId + 1
			EndIf
		'Next
			mapElem = mapElem.NextSibling()
		Wend
		loadedobject = Object(coreMaterial)
		loaded = True
		Return loadedobject	
			
	End	Method
	

End Type 


Type XMLCalMeshLoader Extends CalLoader

	Method Load:Object(data:Object)
		Local isimvu:Byte = False
		
		Local databank:TBank = TBank(data)
		If databank = Null Then
		
			databank = LoadBank(data.ToString())
			If databank = Null Then Return Null
		EndIf
		
		
		Local doc:xmlDocument = New xmlDocument
		doc.Load(databank)
				
		If doc = Null Then
			DebugLog "Could not parse XML"
			loaded = True
			Return Null
		EndIf

		
		Local meshElem:xmlNode = doc.Root()
		
		If meshElem = Null Then 
			Print "Mesh Node not found!"
			loaded = True
			Return Null
		EndIf	
			
		If Lower(meshElem.Name) <> "mesh" Then
			If Lower(meshElem.Name) = "header" Then
				meshElem = meshElem.NextSibling()
				isimvu = True
				If Lower(meshElem.Name) <> "mesh" Then
					Print "Wrong Start Node: "+meshElem.Name
					loaded = True
					Return Null				
				EndIf
			Else
				Print "Wrong Start Node: "+meshElem.Name
				loaded = True
				Return Null	
			EndIf
		EndIf
		
		'If meshElem.GetXMLAttribute("version").value <> "1000" And isimvu = False Then
		'	Print "Wrong Version: "+meshElem.GetXMLAttribute("version").value
		'	Return Null
		'Endif	
		
		Local submeshCount:Int = Int(meshElem.Attribute("NUMSUBMESH").value)	
		Local coreMesh:CalCoreMesh = New CalCoreMesh
		
		'Local submeshIds:List<xmlNode> = meshElem.GetChildren("submesh")
		Local submeshElem:xmlNode = meshElem.FirstChild()
		While submeshElem <> Null
		'For Local submeshElem:xmlNode = EachIn submeshIds
			If Lower(submeshElem.Name) = "submesh" Then
				Local coreMaterialThreadId:Int = Int(submeshElem.Attribute("material").Value)
				Local vertexCount:Int = Int(submeshElem.Attribute("NUMVERTICES").Value) 
				Local faceCount:Int = Int(submeshElem.Attribute("NUMFACES").Value)
				Local lodCount:Int = Int(submeshElem.Attribute("NUMLODSTEPS").Value)
				Local springCount:Int = Int(submeshElem.Attribute("NUMSPRINGS").Value)
				Local textureCoordinateCount:Int = Int(submeshElem.Attribute("NUMTEXCOORDS").Value)
				Local morphCount:Int = Int(submeshElem.Attribute("NUMMORPHS").Value)

				Local coreSubmesh:CalCoreSubmesh = New CalCoreSubmesh
				coreSubmesh.setLodCount(lodCount)
				coreSubmesh.setCoreMaterialThreadId(coreMaterialThreadId)
				
				If coreSubmesh.reserve(vertexCount, textureCoordinateCount, faceCount, springCount) = False Then
					Print "Could not reserve arrays"
					Return Null
				EndIf
				
				'Local vertIds:List<xmlNode> = submeshElem.GetChildren("vertex")
				Local vertexId:Int = 0
				Local vertexElem:xmlNode = submeshElem.FirstChild()
				While vertexElem <> Null
				'For Local vertexElem:xmlNode = EachIn vertIds
					If Lower(vertexElem.Name) = "vertex" Then
						Local vertex:CalVertex = New CalVertex
						
						Local posElem:xmlNode = vertexElem.FindChild("POS")
						Local p:String[] = posElem.value.Split(" ")
						
						vertex.position.x = Float(p[0])
						vertex.position.y = Float(p[1])
						vertex.position.z = Float(p[2])
						
						Local normElem:xmlNode = vertexElem.FindChild("NORM")
						Local n:String[] = normElem.value.Split(" ")
						
						vertex.normal.x = Float(n[0])
						vertex.normal.y = Float(n[1])
						vertex.normal.z = Float(n[2])				
						
						'skip collapse for the time being..
						Local textureCoordinateId:Int = 0
						'Local textcoordsIds:List<xmlNode> = vertexElem.GetChildren("texcoord")
						Local texcoordElem:xmlNode = vertexElem.FirstChild()
						'For Local texcoordElem:xmlNode = EachIn textcoordsIds
						While texcoordElem <> Null
							If Lower(texcoordElem.Name) = "texcoord" Then
								Local textureCoordinate:CalTextureCoordinate = New CalTextureCoordinate
								Local tc:String[] = texcoordElem.value.Split(" ")
								
								textureCoordinate.u = Float(tc[0])
								textureCoordinate.v = Float(tc[1])
								
								coreSubmesh.setTextureCoordinate(vertexId, textureCoordinateId, textureCoordinate)
								textureCoordinateId = textureCoordinateId + 1
							EndIf
							texcoordElem = texcoordElem.NextSibling()
						Wend
						'Local influenceIds:List<xmlNode> = vertexElem.GetChildren("influence")
						Local influenceId:Int = 0
						Local influenceElem:xmlNode = vertexElem.FirstChild()
						'For Local influenceElem:xmlNode = EachIn influenceIds
						While influenceElem <> Null
							If Lower(influenceElem.Name) = "influence" Then
								Local influence:CalInfluence = New CalInfluence
								influence.boneId = Int(influenceElem.Attribute("ID").value)
								influence.weight = Float(influenceElem.value)
								vertex.vectorInfluence = vertex.vectorInfluence[..vertex.vectorInfluence.length+1]
								vertex.vectorInfluence[vertex.vectorInfluence.length-1] = influence
								influenceId = influenceId + 1	
							EndIf
							influenceElem = influenceElem.NextSibling()
						Wend				
						
						coreSubmesh.setVertex(vertexId, vertex)
						
						'Local physIds:List<xmlNode> = vertexElem.GetChildren("physique")
						Local physElem:xmlNode = vertexElem.FirstChild()
						While physElem <> Null
						'For Local physElem:xmlNode = EachIn physIds
							If Lower(physElem.Name) = "physique" Then
								Local physicalProperty:CalPhysicalProperty = New CalPhysicalProperty
								physicalProperty.weight = Float(physElem.Value)
								coreSubmesh.setPhysicalProperty(vertexId, physicalProperty)
							EndIf
							physElem = physElem.NextSibling()
						Wend
						
						vertexId = vertexId + 1
					EndIf
					vertexElem = vertexElem.NextSibling()
				Wend
				
				'Local springIds:List<xmlNode> = submeshElem.GetChildren("spring")
				Local springId:Int = 0
				Local springElem:xmlNode = submeshElem.FirstChild()
				While springElem <> Null
				'For Local springElem:xmlNode = EachIn springIds			
					If Lower(springElem.Name) = "spring" Then
						Local spring:CalSpring = New CalSpring
						Local r:String[] = springElem.Attribute("VERTEXID").Value.Split(" ")
					
						spring.vertexId[0] = Int(r[0])
						spring.vertexId[1] = Int(r[1])
						spring.springCoefficient = Float(springElem.Attribute("COEF").Value)
						spring.idleLength = Float(springElem.Attribute("LENGTH").Value)
						
						coreSubmesh.setSpring(springId, spring)
						springId = springId + 1
					EndIf
					springElem = springElem.NextSibling()
				Wend
	
	
				'Local morphIds:List<xmlNode> = submeshElem.GetChildren("morph")
				Local morphId:Int = 0
				Local morphElem:xmlNode = submeshElem.FirstChild()
				While morphElem <> Null
				'For Local morphElem:xmlNode = EachIn morphIds		
					If Lower(morphElem.Name) = "morph" Then
						Local morphTarget:CalCoreSubMorphTarget = CalCoreSubMorphTarget.Create(morphElem.Attribute("NAME").Value)
						
						morphTarget.reserve(vertexCount)
						
						'Local blendvertIds:List<xmlNode> = morphElem.GetChildren("blendvertex")
						
						For Local i:Int = 0 To vertexCount-1
							Local bvertex:CalBlendVertex = New CalBlendVertex
							Local blendvertexId:Int = 0
							Local blendvertexElem:xmlNode = morphElem.FirstChild()
							While blendvertexElem <> Null
							'For Local blendvertexElem:xmlNode = EachIn blendvertIds				
								If Lower(blendvertexElem.Name) = "blendvertex" Then
									Local vid:Int = Int(blendvertexElem.Attribute("VERTEXID").Value)
									
									If vid = i Then
										
										Local mposElem:xmlNode = blendvertexElem.FindChild("POSITION")
										Local bp:String[] = mposElem.value.Split(" ")
										
										bvertex.position.x = Float(bp[0])
										bvertex.position.y = Float(bp[1])
										bvertex.position.z = Float(bp[2])
										
										Local mnormElem:xmlNode = blendvertexElem.FindChild("NORMAL")
										Local bn:String[] = mnormElem.value.Split(" ")
										
										bvertex.normal.x = Float(bn[0])
										bvertex.normal.y = Float(bn[1])
										bvertex.normal.z = Float(bn[2])								
										DebugLog "Setting Blend Vert: "+i
										
										morphTarget.setBlendVertex(i, bvertex)
									EndIf
								
								
									blendvertexId = blendvertexId + 1
								EndIf
								blendvertexElem = blendvertexElem.NextSibling()
							Wend
						
						Next
						DebugLog "Added Morph: "+morphTarget.m_name
						coreSubmesh.addCoreSubMorphTarget(morphTarget)
						morphId = morphId + 1
					EndIf
					morphElem = morphElem.NextSibling()
				Wend
				
				Local faceId:Int = 0
				'Local faceIds:List<xmlNode> = submeshElem.GetChildren("face")
				Local faceElem:xmlNode = submeshElem.FirstChild()
				While faceElem <> Null
				'For Local faceElem:xmlNode = EachIn faceIds
					If Lower(faceElem.Name) = "face" Then
						Local face:CalFace = New CalFace
						Local fi:String[] = faceElem.Attribute("VERTEXID").value.Split(" ")
						face.vertexId[0] = Int(fi[0])
						face.vertexId[1] = Int(fi[1])
						face.vertexId[2] = Int(fi[2])
						
						coreSubmesh.setFace(faceId, face)
						faceId = faceId + 1
					EndIf
					faceElem = faceElem.NextSibling()
				Wend
				'Next
				coreMesh.addCoreSubmesh(coreSubmesh)
			EndIf
			submeshElem = submeshElem.NextSibling()
		Wend
			
		loadedobject = Object(coreMesh)
		loaded = True
		Return loadedobject		
	End Method

End Type


Type XMLCalSkeletonLoader Extends CalLoader

	Method Load:Object(data:Object)
		Local isimvu:Byte = False
		
		Local databank:TBank = TBank(data)
		If databank = Null Then
		
			databank = LoadBank(data.ToString())
			If databank = Null Then Return Null
		EndIf
		
		
		Local doc:xmlDocument = New xmlDocument
		doc.Load(databank)
				
		If doc = Null Then
			DebugLog "Could not parse XML"
			loaded = True
			Return Null
		EndIf
		
		
		Local skeletonElem:xmlNode = doc.Root()' = doc.GetChild()
		
		If skeletonElem = Null Then 
			DebugLog "Skeleton Node not found!"
			loaded = True
			Return Null
		EndIf
		
		If Lower(skeletonElem.Name) <> "skeleton" Then
			If Lower(skeletonElem.Name) = "header" Then
				skeletonElem = skeletonElem.NextSibling()
				isimvu = True
				If Lower(skeletonElem.Name) <> "skeleton" Then
					DebugLog "Wrong Start Node: "+skeletonElem.Name
					loaded = True
					Return Null				
				EndIf
			Else
				DebugLog "Wrong Start Node: "+skeletonElem.name
				loaded = True
				Return Null	
			EndIf
		EndIf
		
		'If skeletonElem.Attribute("VERSION").value <> "1000" And isimvu = False Then
		'	DebugLog "Wrong Version: "+skeletonElem.GetXMLAttribute("version").value
		'	Return Null
		'EndIf
		
		Local coreSkeleton:CalCoreSkeleton = New CalCoreSkeleton
		
		Local boneCount:Int = Int(skeletonElem.Attribute("NUMBONES").Value)
		'Local boneids:List<xmlNode> = skeletonElem.GetChildren("bone")
		Local boneElem:xmlNode = skeletonElem.FirstChild()
		While boneElem <> Null
		'For Local boneElem:xmlNode = EachIn boneids	
			If Lower(boneElem.Name) = "bone" Then
				Local name:String = boneElem.Attribute("NAME").value
			
				Local translationElem:xmlNode = boneElem.FindChild("TRANSLATION")
				If translationElem = Null Then
					DebugLog "No Translation bone data"
					Return Null
				EndIf
				
				Local r:String[] = translationElem.Value.Split(" ")
				Local tx:Float,ty:Float,tz:Float
				tx = Float(r[0])
				ty = Float(r[1])
				tz = Float(r[2])
				
				Local rotationElem:xmlNode = boneElem.FindChild("ROTATION")
				If rotationElem = Null Then
					DebugLog "No Rotation bone data"
					Return Null
				EndIf
				
				Local rr:String[] = rotationElem.value.Split(" ") 
				Local rx:Float,ry:Float,rz:Float,rw:Float
				
				rx = Float(rr[0])
				ry = Float(rr[1])
				rz = Float(rr[2])
				rw = Float(rr[3])
				
				Local translationBoneSpaceElem:xmlNode = boneElem.FindChild("LOCALTRANSLATION")
				
				If translationBoneSpaceElem = Null Then
					DebugLog "No Local Translation bone data"
					Return Null
				EndIf
				
				
				Local txBoneSpace:Float, tyBoneSpace:Float, tzBoneSpace:Float
				Local tl:String[] = translationBoneSpaceElem.value.Split(" ")
				
				txBoneSpace = Float(tl[0])
				tyBoneSpace = Float(tl[1])
				tzBoneSpace = Float(tl[2])
				
				
				Local rotationBoneSpaceElem:xmlNode = boneElem.FindChild("LOCALROTATION")
	
				If rotationBoneSpaceElem = Null Then
					DebugLog "No Local Rotation bone data"
					Return Null
				EndIf
				
				Local lr:String[] = rotationBoneSpaceElem.value.Split(" ") 
				Local rxBoneSpace:Float, ryBoneSpace:Float, rzBoneSpace:Float, rwBoneSpace:Float
				
				rxBoneSpace = Float(lr[0])
				ryBoneSpace = Float(lr[1])
				rzBoneSpace = Float(lr[2])
				rwBoneSpace = Float(lr[3])
				
				Local parentElem:xmlNode = boneElem.FindChild("PARENTID")
				
				Local parentId:Int = Int(parentElem.value)
				
				
				
				
				Local coreBone:CalCoreBone = CalCoreBone.Create(name)
				coreBone.setParentId(parentId)
				
				
				
				
				Local trans:CalVector = CalVector.FromCoords(tx, ty, tz)
				Local rot:CalQuaternion = CalQuaternion.FromCoords(rx, ry, rz, rw)
				
				'Rotate Base Bone!
				
				If parentId = -1 Then
					Local x_axis_90:CalQuaternion = CalQuaternion.FromCoords(0.7071067811, 0, 0, 0.7071067811)
					rot.multQuaternionLocal(x_axis_90)
					trans.multQuaternionLocal(x_axis_90)
				
				EndIf
				
				coreBone.setTranslation(trans)
				coreBone.SetRotation(rot)
				
				coreBone.setTranslationBoneSpace(CalVector.FromCoords(txBoneSpace, tyBoneSpace, tzBoneSpace))
				coreBone.setRotationBoneSpace(CalQuaternion.FromCoords(rxBoneSpace, ryBoneSpace, rzBoneSpace, rwBoneSpace))								
				
				'Local childids:List<xmlNode> = boneElem.GetChildren("childid")
				'For Local childElem:xmlNode = EachIn childids
				Local childElem:xmlNode = boneElem.FirstChild()
				While childElem <> Null
					
					If Lower(childElem.Name) = "childid" Then
						Local childId:Int = Int(childElem.Value)
						coreBone.addChildId(childId)
					EndIf
					childElem = childElem.NextSibling()
				Wend
				coreBone.setCoreSkeleton(coreSkeleton)
				coreSkeleton.addCoreBone(coreBone)	
			EndIf	
			boneElem = boneElem.NextSibling()
		Wend
		'Next
		coreSkeleton.calculateState()
		
		loadedobject = Object(coreSkeleton)
		loaded = True
		Return loadedobject
	End Method

End Type


Type XMLCalAnimationLoader Extends CalLoader
	
	Field skel:CalCoreSkeleton
	
	
	Method Load:Object(data:Object)
		Local isimvu:Byte = False
		
		Local databank:TBank = TBank(data)
		If databank = Null Then
		
			databank = LoadBank(data.ToString())
			If databank = Null Then Return Null
		EndIf
		
		
		Local doc:xmlDocument = New xmlDocument
		doc.Load(databank)
				
		If doc = Null Then
			DebugLog "Could not parse XML"
			loaded = True
			Return Null
		EndIf
		

		Local animationElem:xmlNode = doc.Root()
		
		If animationElem = Null Then 
			DebugLog "Animation Node not found!"
			loaded = True
			Return Null
		EndIf	
			
		If Lower(animationElem.name) <> "animation" Then
			If Lower(animationElem.name) = "header" Then
				animationElem = animationElem.NextSibling()
				isimvu = True
				If Lower(animationElem.name) <> "animation" Then
					DebugLog "Wrong Start Node: "+animationElem.name
					loaded = True
					Return Null				
				EndIf
			Else
				DebugLog "Wrong Start Node: "+animationElem.name
				loaded = True
				Return Null	
			EndIf
		EndIf	
		
		
		'If animationElem.Attribute("version").value <> "1000" And isimvu = False Then
	'		Print "Wrong Version: "+animationElem.GetXMLAttribute("version").value
	'		Return Null
	'	EndIf
		
		Local duration:Float = Float(animationElem.Attribute("DURATION").value)
		Local trackCount:Int = Int(animationElem.Attribute("NUMTRACKS").value)
		
		Local coreAnimation:CalCoreAnimation = New CalCoreAnimation
		
		If duration <=0 Then
			DebugLog "Invalid Duration"
			Return Null
		EndIf
		
		coreAnimation.setDuration(duration)
		'Local trackIds:List<xmlNode> = animationElem.GetChildren("track")
		Local trackElem:xmlNode = animationElem.FirstChild()
		While trackElem <> Null
		'For Local trackElem:xmlNode = EachIn trackIds
			If Lower(trackElem.Name) = "track" Then
				Local coreTrack:CalCoreTrack = New CalCoreTrack
				
				Local coreBoneId:Int = Int(trackElem.Attribute("BONEID").value)
				coreTrack.setCoreBoneId(coreBoneId)
				
				Local keyframeCount:Int = Int(trackElem.Attribute("NUMKEYFRAMES").value)
				
				'Local keyIds:List<xmlNode> = trackElem.GetChildren("keyframe")
				Local keyframeElem:xmlNode = trackElem.FirstChild()
				While keyframeElem <> Null
				'For Local keyframeElem:xmlNode = EachIn keyIds
					If Lower(keyframeElem.Name) = "keyframe" Then
						Local time:Float = Float(keyframeElem.Attribute("TIME").value)
						
						Local translationElem:xmlNode = keyframeElem.FindChild("TRANSLATION")
						
						Local tr:String[]
						If translationElem = Null Then
							tr = tr[..3]
						Else
							tr = translationElem.value.Split(" ")
						EndIf
						
						Local tx:Float, ty:Float, tz:Float
						
						tx = Float(tr[0])
						ty = Float(tr[1])
						tz = Float(tr[2])
						
						Local trans:CalVector = CalVector.FromCoords(tx, ty, tz)
						
						Local rotationElem:xmlNode = keyframeElem.FindChild("ROTATION")
						Local ro:String[] = rotationElem.value.Split(" ")
						Local rx:Float, ry:Float, rz:Float, rw:Float
						
						rx = Float(ro[0])
						ry = Float(ro[1])
						rz = Float(ro[2])
						rw = Float(ro[3])
						Local rot:CalQuaternion = CalQuaternion.FromCoords(rx, ry, rz, rw)
						
						
						If skel <> Null Then
						
							If skel.getCoreBonebyID(coreBoneId).getParentId() = -1 Then
						
								Local x_axis_90:CalQuaternion = CalQuaternion.FromCoords(0.7071067811, 0, 0, 0.7071067811)
								rot.multQuaternionLocal(x_axis_90)
								trans.multQuaternionLocal(x_axis_90)
								
							EndIf
						EndIf
						
						Local coreKeyframe:CalCoreKeyframe = New CalCoreKeyframe
						coreKeyframe.setTime(time)
						coreKeyframe.setTranslation(trans)
						coreKeyframe.SetRotation(rot)
						coreTrack.addCoreKeyframe(coreKeyframe)
					EndIf
					keyframeElem = keyframeElem.NextSibling()
				Wend
				coreAnimation.addCoreTrack(coreTrack)
			EndIf
			trackElem = trackElem.NextSibling()
		Wend
		loadedobject = Object(coreAnimation)
		loaded = True
		Return loadedobject		
				
			
		
	End Method


End Type

Type XMLMorphCalAnimationLoader Extends CalLoader
	
	
	
	Method Load:Object(data:Object)
		Local isimvu:Byte = False
		
		Local databank:TBank = TBank(data)
		If databank = Null Then
		
			databank = LoadBank(data.ToString())
			If databank = Null Then Return Null
		EndIf
		
		
		Local doc:xmlDocument = New xmlDocument
		doc.Load(databank)
				
		If doc = Null Then
			DebugLog "Could not parse XML"
			loaded = True
			Return Null
		EndIf
		

		Local animationElem:xmlNode = doc.Root()

		If animationElem = Null Then 
			DebugLog "Animation Node not found!"
			loaded = True
			Return Null
		EndIf	
			
		If Lower(animationElem.name) <> "animation" Then
			If Lower(animationElem.name) = "header" Then
				animationElem = animationElem.NextSibling()
				isimvu = True
				If Lower(animationElem.name) <> "animation" Then
					DebugLog "Wrong Start Node: "+animationElem.name
					loaded = True
					Return Null				
				EndIf
			Else
				DebugLog "Wrong Start Node: "+animationElem.name
				loaded = True
				Return Null	
			EndIf
		EndIf

		Local duration:Float = Float(animationElem.Attribute("DURATION").value)
		Local trackCount:Int = Int(animationElem.Attribute("NUMTRACKS").value)
		
		Local coreAnimation:CalCoreAnimatedMorph = New CalCoreAnimatedMorph
		
		If duration <=0 Then
			DebugLog "Invalid Duration"
			Return Null
		EndIf
		
		coreAnimation.setDuration(duration)
		Local trackElem:xmlNode = animationElem.FirstChild()
		While trackElem <> Null
		'For Local trackElem:xmlNode = EachIn trackIds
			If Lower(trackElem.Name) = "track" Then
				Local coreTrack:CalCoreMorphTrack = New CalCoreMorphTrack
				Local coreMorphName:String = trackElem.Attribute("MORPHNAME").value
				
				coreTrack.setMorphName(coreMorphName)
				Local keyframeCount:Int = Int(trackElem.Attribute("NUMKEYFRAMES").value)
				Local keyframeElem:xmlNode = trackElem.FirstChild()
				While keyframeElem <> Null
				'For Local keyframeElem:xmlNode = EachIn keyIds
					If Lower(keyframeElem.Name) = "keyframe" Then
						Local time:Float = Float(keyframeElem.Attribute("TIME").value)
						DebugLog "Keytime: "+time
						Local weightElem:xmlNode = keyframeElem.FindChild("WEIGHT")
						Local weight:Float = Float(weightElem.Value)
						Local coreKeyframe:CalCoreMorphKeyframe = New CalCoreMorphKeyframe
						coreKeyframe.setTime(time)
						coreKeyframe.setWeight(weight)
						coreTrack.addCoreMorphKeyframe(coreKeyframe)
					EndIf
					keyframeElem = keyframeElem.NextSibling()
				Wend
				coreAnimation.addCoreTrack(coreTrack)
				
			EndIf
			trackElem = trackElem.NextSibling()
		Wend


		loadedobject = Object(coreAnimation)
		loaded = True
		Return loadedobject		
				
			
		
	End Method
	
End Type
